package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.ConvertHttpToMap;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import com.sinog.front.config.idempotent.Idempotent;
import com.sinog.front.config.log.Log;
import com.sinog.front.feign.biz.BaseInfoService;
import com.sinog.front.feign.biz.BaseInfoblService;
import com.sinog.front.service.user.UserFacade;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @desc 安置帮教管理 - 人员信息补录 - 信息补录 InmatesBlController
 * @author yanhao
 * @date 2021/4/27 15:44
 **/
@RestController
@Slf4j
public class InmatesBlController {

    @Resource
    private UserFacade userService;
    @Resource
    private BaseInfoblService baseInfoblService;
    @Resource
    private BaseInfoService baseInfoService;

    /**
     * 安置帮教管理 - 人员信息补录 - 信息录入 - 监狱释放人员录入 - 补录基本信息
     * @param request request
     * @param data 业务数据
     * @return Result
     */
    @Log(type = "更新", value = "补录罪犯基本信息")
    @Idempotent(paramPos = 1)
    @PostMapping("/azbj/inmates_bl/baseinfo/save")
    public Result saveBaseInfo(HttpServletRequest request,
                               @RequestParam(required = false, defaultValue = "-1") String data) {
        UserDTO user = userService.getAuthentication();
        String id = JsonUtil.getValueFromJsonstrByKey(data, Constants.StrFinalValue.ID);
        //修改基本信息
        if(StringUtils.isNotBlank(id)) {
            return baseInfoService.saveBaseInfo(data,user,Constants.NumberStr.NUM_STR_THREE);
        }
        //保存该服刑人员
        return baseInfoService.saveBaseInfo(data,user,Constants.NumberStr.NUM_STR_TWO);
    }

    /**
     * 安置帮教管理 - 人员信息补录 - 信息补录 - 列表
     * @param request request
     * @param pageIndex 起始页
     * @param pageSize 止页
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @return String
     */
    @RequestMapping("/azbj/inmates_bl/info/list")
    public String getInmatesList(HttpServletRequest request,Integer pageIndex,Integer pageSize,String sortField,String sortOrder) {
        UserDTO user = userService.getAuthentication();
        Map<String,Object> parameterMap = ConvertHttpToMap.getParameterMap(request);
        parameterMap.put("departid",user.getDepartid());
        parameterMap.put("sortField",sortField);
        parameterMap.put("sortOrder",sortOrder);
        return baseInfoblService.getBlInmatesList(pageIndex,pageSize,parameterMap);
    }

    /**
     * 获取户籍信息
     * @return Result
     */
    @GetMapping("/azbj/inmates_bl/getHuJi")
    public Result getHuJi() {
        UserDTO userDTO = userService.getAuthentication();
        String level = userDTO.getLevel();
        String departId = userDTO.getDepartid();
        return baseInfoblService.getHuJi(level,departId);
    }

    /**
     * 安置帮教管理 - 人员信息补录 - 信息补录 - 删除按钮
     * @param prinum 服刑人员编号
     * @return Result
     */
    @Log(type = "删除", value = "根据罪犯编号删除罪犯基本信息")
    @PostMapping("/azbj/inmates/baseinfo/deleteInfo")
    public Result deleteBaseinfoByPriNum(HttpServletRequest request,@RequestParam(required = false, defaultValue = "-1") String prinum) {
        int num = baseInfoblService.remeveBaseinfoByPriNum(prinum);
        if(Constants.Number.NUM_ZERO < num) {
            return Result.success();
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }

    /**
     * 安置帮教管理 - 人员信息补录 - 信息补录 - 批量删除
     * @param prinumLists 服刑人员编号集合
     * @return Result
     */
    @Log(type = "删除", value = "批量删除罪犯基本信息")
    @PostMapping("/azbj/inmates/baseinfo/deleteInfos")
    public Result deleteBaseinfoByPriNums(HttpServletRequest request,
            @RequestParam(name = "prinumLists", required = false) List<String> prinumLists) {
        try {
            baseInfoblService.remeveBaseinfoByPriNums(prinumLists);
            return Result.success();
        } catch(Exception e) {
            return Result.failure(ReturnCode.DELETE_FAILED);
        }
    }

    /**
     * 根据priNum查找是否存在服刑信息
     * @param priNum priNum
     * @return Result
     */
    @GetMapping("/azbj/inmates_bl/getFxByPriNum/{priNum}")
    public Result getFxByPriNum(@PathVariable String priNum) {
        int count = baseInfoblService.getFxByPriNum(priNum);
        if(Constants.Number.NUM_ZERO < count) {
            return Result.success(1);
        }
        return Result.failure(ReturnCode.FAILURE);
    }
}